---
import ArticlePage from "@components/cyue-share/ArticlePage.astro";
import type { Page } from "astro";
import { ArticleInformation } from "@/types/article";
import { WebResult } from "@/types/web-result";
import Pagination from "../components/control/Pagination.astro";
import { PAGE_SIZE } from "../constants/constants";
import MainGridLayout from "../layouts/MainGridLayout.astro";
import httpRequest from "../utils/http";

const { page } = Astro.params;
const currentPage = Number(page || 1);

const { data: articleList, page: pageResult } = await httpRequest<
	WebResult<ArticleInformation[]>
>({
	url: `/api/article/page/${currentPage}?pageSize=${PAGE_SIZE}&with_content=true`,
}).then((result) => {
	if (result && !result.data) {
		result.data = [];
	}
	return result || { data: [], page: { page: 1, pageSize: PAGE_SIZE, total: 0, totalPage: 1 } };
});
const totalPage = pageResult?.totalPage || 1;
const pagination: Page = {
	data: [],
	start: 0,
	end: PAGE_SIZE,
	total: pageResult?.total || 0,
	currentPage,
	size: PAGE_SIZE,
	lastPage: totalPage,
	url: {
		current: `/${currentPage}/`,
		next: `/${currentPage + 1}/`,
		prev: `/${currentPage - 1}/`,
		first: "/",
		last: `/${totalPage}/`,
	},
};
---

<MainGridLayout>
	<ArticlePage articleList={articleList!}></ArticlePage>
    <Pagination
        class="mx-auto onload-animation"
        style={`animation-delay: calc(var(--content-delay) + ${articleList!.length * 50}ms)`}
        page={pagination}
    ></Pagination>
</MainGridLayout>
